﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Basis.Help
{
    /// <summary>
    /// 反射帮助类
    /// </summary>
    public class ReflectionHelper
    {
        /// <summary>
        /// 获取对象属性的值,属性路径支持嵌套,用"."隔开
        /// </summary>
        /// <param name="target">目标对象</param>
        /// <param name="propertyPath">属性路径</param>
        /// <returns></returns>
        public static object GetPropertyValue(object target, string propertyPath)
        {
            if (target == null) return null;
            if (propertyPath == null || propertyPath.Trim() == "") return target;
            string[] pros = propertyPath.Split(new char[] { '.' });
            object current = target;
            foreach (var k in pros)
            {
                PropertyInfo pi = current.GetType().GetProperty(k);
                current = pi.GetValue(current, null);
            }
            return current;
        }

        public static T GetPropertyValue<T>(object target, string propertyPath)
        {
            return (T)ReflectionHelper.GetPropertyValue(target, propertyPath);
        }




    }
}
